// Package service @program: gateway
//@author: edte
//@create: 2021-12-29 14:28
package service

import (
	"gateway/client/account"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

type admin struct {
	Username string `json:"Username"`
	Password string
}

func AdminLogin(ct *gin.Context) {
	json := admin{}
	err := ct.BindJSON(&json)
	if err != nil || json.Username == "" || json.Password == "" {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "请求格式不对"})
		return
	}
	login, err := account.AdminLogin(json.Username, json.Password)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest, login)
		return
	}
	ct.JSON(http.StatusOK, login)
}

type adminregister struct {
	Token    string
	Username string
	Password string
	Lever    int
}

func AdminRegister(ct *gin.Context) {
	json := adminregister{}
	err := ct.BindJSON(&json)
	if err != nil || json.Token == "" || json.Username == "" || json.Lever == 0 || json.Password == "" {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "请求格式不对"})
		return
	}
	register, err := account.AdminRegister(json.Token, json.Username, json.Password, json.Lever)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest, register)
		return
	}
	ct.JSON(http.StatusOK, register)
}

type adminquery struct {
	Token   string
	StartId int
	EndId   int
}

func AdminQuery(ct *gin.Context) {
	json := adminquery{}
	err := ct.BindJSON(&json)
	if err != nil || json.Token == "" || json.StartId == 0 || json.EndId == 0 {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "请求格式不对"})
		return
	}
	data, err := account.AdminQueryData(json.Token, json.StartId, json.EndId)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest, data)
		return
	}
	ct.JSON(http.StatusOK, data)
}

type blockuser struct {
	Token  string
	UserId int
}

func BlockUser(ct *gin.Context) {
	json := blockuser{}
	err := ct.BindJSON(&json)
	if err != nil || json.Token == "" || json.UserId == 0 {
		ct.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "请求格式不对"})
		return
	}
	user, err := account.BlockUser(json.Token, json.UserId)
	if err != nil {
		log.Println(err)
		ct.JSON(http.StatusBadRequest, user)
		return
	}
	ct.JSON(http.StatusOK, user)
}
